<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Events\OpenShop\ShopCreatedOnceEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\Feishu\UserExtContractService;
use Illuminate\Support\Facades\Log;

class ShopCreateContractListener extends BaseListener
{
    /**
     * 生成契约锁合同
     *
     * @return void 
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param \App\Events\OpenShop\ShopCreatedOnceEvent $event
     * @return void
     */
    public function handle(ShopCreatedOnceEvent $event)
    {
        Log::channel('listener')
           ->info(__CLASS__ . " handle contract...orderNo:" . $event->orderNo);

        $order = OpenShopSignOrderModel::query()
                                       ->where('orderNo', $event->orderNo)
                                       ->first();
        if (null === $order) {
            return;
        }

        // 不创建契约锁合同
        if ($order->orderType == SignOrderEnum::ORDER_TYPE_IMPORT) {
            Log::channel('listener')->info(__CLASS__ . " skip contract...orderNo:" . $event->orderNo);
            return;
        }

        UserExtContractService::orderNotify($event->orderNo);

        Log::channel('listener')
           ->info(__CLASS__ . " handle finish contract...orderNo:" . $event->orderNo);
    }
}
